$(function() {
	require([ '/JWLetter/js/dist/theme/blue' ], function(tarTheme) {
		// 路径配置
		require.config({
			paths : {
				echarts : '/JWLetter/js/dist'
			}
		});
		// 使用
		require(
			[
				'echarts',
				'echarts/chart/bar',
				'echarts/chart/line', // 使用柱状图就加载bar模块，按需加载
				'echarts/chart/force',
				'echarts/chart/chord'
			],
			//将画多个图表的进行函数封装
			function drawCharts(ec) {
				drawRelationGraph(ec);
			}
		);
	})
	
	//上一页
	$("#preview").bind("click",function() {
		var pageNum = parseInt($("#current").val());
		var sumPage = parseInt($("#sum").val());
		if (1 < pageNum && pageNum <= sumPage) {
			var letterIds = $("#temp").val();
			loadRelationLetters(pageNum - 1, letterIds);
		}
	});
	
	//下一页
	$("#next").bind("click",function(){
		var pageNum = parseInt($("#current").val());
		var sumPage = parseInt($("#sum").val());
		if (1 <= pageNum && pageNum < sumPage) {
			var letterIds = $("#temp").val();
			loadRelationLetters(pageNum + 1, letterIds);
		}
	});
	
});

/**
 * 画出关系图谱
 */
function drawRelationGraph(ec) {
	var url = "/JWLetter/graph/loadGraph";
	var data = {
		word : $("#word").val()
	}
	var successCallback = function(data){
		$("#temp").attr("value",data.detail.letterIds);
		treeChart = ec.init(document.getElementById('tree'), 'macarons');
		var node = data.detail.nodes;
		var link = data.detail.links;
		var ecConfig = require('echarts/config');
		treeChart.on(ecConfig.EVENT.CLICK, clickFuns);
		var option = {
			title : {
				text : '',
				subtext : '',
				x : 'right',
				y : 'bottom'
			},
			tooltip : {
				trigger : 'item',
				formatter : '{a} : {b}'
			},
			toolbox : {
				show : true,
				feature : {
					restore : {
						show : true
					},
					magicType : {
						show : true,
						type : [ 'force', 'chord' ]
					},
					saveAsImage : {
						show : true
					}
				}
			},
			legend : {
				x : 'left',
				data : [ '人名', '地名', '组织机构', '罪名', '职务', '诉讼日期', '其他' ],
				textStyle : {
					fontFamily : '微软雅黑'
				}
			},
			series : [
				{
					type : 'force',
					name : "关联关系",
					ribbonType : false,
					clickable : true,
					categories : [
						{
							name : '关键词'
						}, {
							name : '人名'
						}, {
							name : '地名'
						}, {
							name : '组织机构'
						}, {
							name : '罪名'
						}, {
							name : '职务'
						}, {
							name : '诉讼日期'
						}, {
							name : '其他'
						}
					],

					itemStyle : {
						normal : {
							label : {
								show : true,
								textStyle : {
									color : '#eee'
								}
							},
							nodeStyle : {
								brushType : 'both',
								borderColor : 'rgba(0,0,0,0)',
								borderWidth : 1
							},
							linkStyle : {
								type : 'curve',
								color : 'rgb(145,146,148)',
								borderColor : '#919294'
							}
						},
						emphasis : {
							label : {
								show : false
							},
						}
					},
					useWorker : false,
					minRadius : 20,
					maxRadius : 35,
					gravity : 1,
					scaling : 1.6,
					roam : 'move',
					nodes : data.detail.nodes,
					links : data.detail.links
				} ]
		};
		treeChart.setOption(option);
		loadRelationLetters(1,$("#temp").val());
	};
	JWUtils.ajax(url, false, data, successCallback, null);
}

/**
 * 实体和关系点击事件
 */
function clickFuns(param) {
	if (typeof param.data.source == 'undefined') {//实体点击事件
		if (param.type == 'click') {
			window.location.href="/JWLetter/forward/graph?word="+param.data.name;
		}
	} else {
		if (param.type == 'click') {//关系点击事件
			$("#letterIds").attr("value",param.data.letterIds);
			loadRelationLetters(1,$("#letterIds").val());
		}
	}
}

/**
 * 加载相关信件信息
 */
function loadRelationLetters(pageNum,letterIds) {
	var url = "/JWLetter/graph/loadRelationLetters";
	var data = {
		pageNum : pageNum,
		letterIds : letterIds
	}
	var successCallback = function(data){
		$("#current").attr("value",data.detail.pageResult.currentPage);
		$("#sum").attr("value",data.detail.pageResult.sumPage);
		var pageResult = data.detail.pageResult;
		if (pageResult.currentPage == pageResult.sumPage) {
			$("#recordsNums").text(((pageResult.currentPage - 1) * pageResult.perPage + 1) + "-" + pageResult.total + "/" + pageResult.total);
		} else {
			$("#recordsNums").text(((pageResult.currentPage - 1) * pageResult.perPage + 1) + "-" + pageResult.currentPage * pageResult.perPage + "/" + pageResult.total);
		}
		var letters = pageResult.records;
		var div = $("#letters");
		div.children().remove();
		if (letters.length != 0) {
			for (var i = 0; i < letters.length; i++) {
				div.append("<p class='text-muted' ><span class='label label-danger'>"+letters[i].categoryName+"</span><a name='showDetail' letterId='"+letters[i].letterId+"' href='javascript:;'>"+letters[i].title+"</a></p>");
			}
		}
		showDetail();
	};
	JWUtils.ajax(url, false, data, successCallback, null);
}


/**
 * 显示信件详情信息
 */
function showDetail(){
	$("a[name='showDetail']").each(function(){
		$(this).bind("click",function(){
			var letterId = $(this).attr("letterId");
			var url = "/JWLetter/letter/loadLetterDetail";
			var data = {
				letterId : letterId
			};
			var successCallback = function(data){
				$("#map").hide();
				var letter = data.detail.letter;
				$("#title").children().remove();
				$("#title").append("<h3>"+letter.title+"</h3>");
				$("#title").append("<h5><span>"+letter.author+"</span><span class='mailbox-read-time pull-right'>"+letter.formatDate+"</span></h5>");
				$("#content").html(letter.content);
				$("#detail").show();
			};
			JWUtils.ajax(url, false, data, successCallback, null);
		});
	});
}